package com.example.weather_news.fragment;

import android.content.Intent;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.weather_news.R;
import com.example.weather_news.activity.WeatherSettingActivity;
import com.example.weather_news.db.SaveCityInfo;
import com.example.weather_news.fragment.frag_items.Frag_Weather;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class Fragment_Weather extends BaseFragment {

    private TextView tv_weather_cityName;
    public static ImageView iv_weather_back;
    private ImageView iv_weather_setting;
    private ViewPager vp_weather;
    private LinearLayout linear_city;

    private int mIndex = 0;//小圆点当前位置

    private ImageView oval;//原点

    private List<SaveCityInfo> list = new ArrayList<SaveCityInfo>();

//    private MyPagerAdapter adapter;

    private MyPagerAdapter adapter;

    private int citySize = 0;

    @Override
    public void initView() {
        tv_weather_cityName = findView(R.id.tv_weather_cityName);
        iv_weather_back = findView(R.id.iv_weather_back);
        iv_weather_setting = findView(R.id.iv_weather_setting);
        vp_weather = findView(R.id.vp_weather);
        linear_city = findView(R.id.linear_city);

        setOnClick(iv_weather_setting);
    }

    @Override
    public void initData() {
        if (Build.VERSION.SDK_INT >= 21) {
//            View view = getActivity().getWindow().getDecorView();
//            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//            view.setSystemUiVisibility(option);
            this.getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.gray));
        }

//        adapter = new MyPagerAdapter(getChildFragmentManager());
//        vp_weather.setAdapter(adapter);

        adapter=new MyPagerAdapter(getChildFragmentManager());
        vp_weather.setAdapter(adapter);

        tv_weather_cityName.setText("睢宁县");

        vp_weather.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIndex = position;

                tv_weather_cityName.setText(list.get(position).getCityName());
                for (int i = 0; i < linear_city.getChildCount(); i++) {
                    ImageView img = (ImageView) linear_city.getChildAt(i);
                    if (i == position) {
                        img.setImageResource(R.drawable.oval_checked);
                    } else {
                        img.setImageResource(R.drawable.oval_unchecked);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
        //查看以保存的城市
        list.clear();
        list = DataSupport.findAll(SaveCityInfo.class);
        adapter.setData(list);

        vp_weather.setCurrentItem(DataSupport.findAll(SaveCityInfo.class).size());

        setOval();
        Log.e("b", citySize + "\t" + DataSupport.findAll(SaveCityInfo.class).toString());
    }

    //设置导航原点
    private void setOval() {
        linear_city.removeAllViews();
        for (int i = 0; i < list.size(); i++) {
            oval = new ImageView(getActivity());
            if (i == mIndex) {
                if (i == mIndex) {
                    oval.setImageResource(R.drawable.oval_checked);
                } else {
                    oval.setImageResource(R.drawable.oval_unchecked);
                }
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
            if (i > 0) {
                params.leftMargin = 15;
            }
            oval.setLayoutParams(params);
            linear_city.addView(oval);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (Build.VERSION.SDK_INT >= 21) {
//                View view = getActivity().getWindow().getDecorView();
//                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//                view.setSystemUiVisibility(option);
                this.getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color
                        .gray));
            }
        }
    }

    //继承FragmentStatePagerAdapter可以对动态更新页面更加容易，FragmentPagerAdapter会有对Fragment的缓存，不容易清除
    class MyPagerAdapter extends FragmentStatePagerAdapter {
        private List<SaveCityInfo> lists = new ArrayList<SaveCityInfo>();

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public void setData(List<SaveCityInfo> lists) {
            this.lists = lists;
            this.notifyDataSetChanged();
        }
        @Override
        public Fragment getItem(int position) {
            if (lists.get(position) != null) {
                return Frag_Weather.inseryWeather(lists.get(position).getCityName());
            }
            return null;
        }

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;//POSITION_NONE:没有找到child要求重新加载。
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_weather_setting:
                startActivity(new Intent(getActivity(), WeatherSettingActivity.class));
                getActivity().
                        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                break;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_weather;
    }

}
